package com.stone.sys.action;

/**
 * <pre>
 * 字典信息维护
 * @author create
 * </pre>
 */

import java.io.IOException;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.sys.pojo.TDicType;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.service.DictionaryService;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class DictionaryAction extends BaseAction {

	private static final long serialVersionUID = -4647594073681054487L;

	private DictionaryService dicService;

	/**
	 * 字典
	 */
	private TDictionary dic;

	private TDicType dictype;

	/**
	 * ID
	 */
	private String id;

	/**
	 * 字典列表
	 */
	private List<TDictionary> dicList;

	private String pagetype;

	/**
	 * 进入详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {

		// 字典类型
		if ("dictype".equals(this.pagetype)) {

			// 修改进入
			if (!"0".equals(id)) {

				this.dictype = (TDicType) dicService.findById(Long
						.parseLong(id), TDicType.class.getName());

				// 新建进入
			} else {
				this.dictype = new TDicType();
				this.dictype.setId(Long.parseLong("0"));
				// this.dic.setItemType(super.getSearchBean().getCon1());

			}

			return "DIC_TYPE_SUCCESS";

		} else {

			// 修改进入
			if (!"0".equals(id)) {

				this.dic = (TDictionary) dicService.findById(
						Long.parseLong(id), TDictionary.class.getName());

				// 新建进入
			} else {
				// this.dic = new TDictionary();
				this.dic.setId(Long.parseLong("0"));
				// this.dic.setItemType(super.getSearchBean().getCon1());

			}

			return SUCCESS;

		}
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {

		if ("dictype".equals(this.pagetype)) {

			// 新建时
			if (dictype.getId() == 0) {
				dicService.save(dictype);
				// 修改排序号
				dicService.updateByHQL(
						"update TDicType set sortid = ? where id = ?",
						new Object[] { dictype.getId(), dictype.getId() });
			} else {

				dicService.update(dictype);

			}

			super.setInfo("保存成功");

			return "DIC_TYPE_SUCCESS";

		} else {

			// 新建时
			if (dic.getId() == 0) {
				dicService.save(dic);
				// 修改排序号
				dicService.updateByHQL(
						"update TDictionary set sortid = ? where id = ?",
						new Object[] { dic.getId(), dic.getId() });
			} else {

				dicService.update(dic);

			}

			super.setInfo("保存成功");

			return SUCCESS;

		}

	}

	/**
	 * 验证
	 * 
	 * @return
	 * @throws AppException
	 * @throws IOException
	 */
	public void check() throws Exception {

		String out = "{}";

		long count = 0;

		// 字典类型
		if ("dictype".equals(this.pagetype)) {

			count = dicService.getTotalByDHql("typecode=?",
					new Object[] { dictype.getTypecode() }, "TDicType");

			// 字典
		} else {

			String itemId = super.getRequest().getParameter("itemId");
			String itemType = super.getRequest().getParameter("itemType");

			// 登陆账号验证
			count = dicService.getTotalByDHql("itemType=? and itemId = ?",
					new Object[] { itemType, itemId }, "TDictionary");

		}

		if (count > 0) {
			out = "{m:'编号已经存在！'}";
		}

		super.getResponse().getWriter().print(out);

	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {

		// 字典类型
		if ("dictype".equals(this.pagetype)) {

			dicService.doDelAll(id, "TDicType");
			return "DIC_TYPE_DEL-SUCCESS";

		} else {

			dicService.doDelAll(id, "TDictionary");

			return "DEL-SUCCESS";
		}

	}

	/**
	 * 进入字典弹出页面
	 */
	public String initDic() {

		dicList = dicService
				.findListByHql(" from TDictionary where itemType = '"
						+ dic.getItemType() + "' order by sortid");

		return "DIC_POPUP_SUCCESS";

	}

	/**
	 * 进入字典弹出页面
	 */
	public void getDicJson() throws Exception {

		dicList = dicService
				.findListByHql(" from TDictionary where itemType in ('"
						+ dic.getItemType() + "') order by sortid");

		if (!StringUtils.isNullOrBlank(super.getRequest().getParameter(
				"emptyFlag"))) {
			TDictionary dic = new TDictionary();
			dic.setItemName("全部");
			dicList.add(0, dic);
		}

		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");

		JSON json = JSONSerializer.toJSON(dicList, jsonConfig);

		super.getResponse().getWriter().print(json.toString());

	}
	
	/**
	 * 进入字典弹出页面
	 */
	public void getEquipDicJson() throws Exception {
		
		String where = "";
		
		// 一次设备
		if ("SBLX1".equals(dic.getItemType())) {
			
			where = " and itemId like '0%'";
			
			// 二次设备
		} else if ("SBLX2".equals(dic.getItemType())) {
			where = " and itemId like 'E%'";
		}

		dicList = dicService
				.findListByHql(" from TDictionary where itemType = 'SBLX'" + where + " order by sortid");

		if (!StringUtils.isNullOrBlank(super.getRequest().getParameter(
				"emptyFlag"))) {
			TDictionary dic = new TDictionary();
			dic.setItemName("全部");
			dicList.add(0, dic);
		}

		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");

		JSON json = JSONSerializer.toJSON(dicList, jsonConfig);

		super.getResponse().getWriter().print(json.toString());

	}

	public DictionaryService getDicService() {
		return dicService;
	}

	public void setDicService(DictionaryService dicService) {
		this.dicService = dicService;
	}

	public TDictionary getDic() {
		return dic;
	}

	public void setDic(TDictionary dic) {
		this.dic = dic;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<TDictionary> getDicList() {
		return dicList;
	}

	public TDicType getDictype() {
		return dictype;
	}

	public void setDictype(TDicType dictype) {
		this.dictype = dictype;
	}

	public void setPagetype(String pagetype) {
		this.pagetype = pagetype;
	}

}
